package com.frontResponse.dao.impl;

import com.frontResponse.dao.VideoDao;
import com.frontResponse.entity.VideoSql;
import com.javasm.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author WangJie
 * @version 1.0
 * @Description:
 * @date: 2020/7/23 21:58
 * @since JDK 1.8
 */
public class VideoDaoImpl implements VideoDao {
    /**
     * 查询视频一级分类所有信息
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> selectVideoTopSort() {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        List<Map<String, Object>> videoTopSortList = null;
        try {
            videoTopSortList = queryRunner.query(VideoSql.SELECT_VIDEO_TOP_SORT, new MapListHandler());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return videoTopSortList;
    }

    /**
     * 根据一级分类id查询二级分类
     *
     * @param tsvid 一级分类id
     * @return
     */
    @Override
    public List<Map<String, Object>> selectVideoSecondSortByTsvid(String tsvid) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        List<Map<String, Object>> videoSecondSortList = null;
        try {
            videoSecondSortList = queryRunner.query(VideoSql.SELECT_VIDEO_SECOND_SORT_BY_TSVID, new MapListHandler(), tsvid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return videoSecondSortList;
    }

    /**
     * 根据二级分类分页查询视频信息
     *
     * @param ssvid 二级分类id
     * @return
     */
    @Override
    public List<Map<String, Object>> selectVideoInfoByPage(String ssvid, int page, int pageSize) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        List<Map<String, Object>> videoInfoList = null;
        try {
            videoInfoList = queryRunner.query(VideoSql.SELECT_VIDEO_INFO_BY_PAGE, new MapListHandler(), ssvid, (page - 1) * pageSize, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return videoInfoList;
    }

    /**
     * 根据视频id查询对应的电影信息
     *
     * @param vid 视频id
     * @return
     */
    @Override
    public List<Map<String, Object>> selectVideoSoloByVid(String vid) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        List<Map<String, Object>> videoSoloInfo = null;
        try {
            videoSoloInfo = queryRunner.query(VideoSql.SELECT_VIDEOSOLO_BY_VID, new MapListHandler(), vid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return videoSoloInfo;
    }

    /**
     * 通过vid查询对应剧集信息
     * @param vid 视频id
     * @return
     */
    @Override
    public List<Map<String, Object>> selectTVPlayByVid(String vid) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        List<Map<String, Object>> TVPlayInfoList = null;
        try {
            TVPlayInfoList = queryRunner.query(VideoSql.SELECT_TVPLAY_BY_VID, new MapListHandler(), vid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return TVPlayInfoList;
    }

    /**
     * 通过vid查询视频简介，为查询剧集信息服务
     *
     * @param vid 视频id
     * @return
     */
    @Override
    public Map<String, Object> selectVideoInfoByVid(String vid) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        Map<String, Object> videoInfo = null;
        try {
            videoInfo = queryRunner.query(VideoSql.SELECT_VIDEO_INFO_BY_VID, new MapHandler(), vid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return videoInfo;
    }
}
